package project5;

import java.io.*;

/**
 * This class handles all File I/O for a byte-by-byte file read.
 * @author Surabhi Diwan
 */
public class FileHandler
{
	FileInputStream fis;

	/**
	 * Constructs a FileHandler object to handle file I/O.
	 * @param file_path The relative or absolute path of the file.
	 */
	public FileInputStream FileHandlerCreator(String file_path)
	{

		try
		{
			fis = new FileInputStream(file_path);

		}
		catch(FileNotFoundException fnfe)
		{
			fnfe.printStackTrace();
			System.out.println("ERROR: unable to process input file.");
			System.exit(0);
		}
		catch(SecurityException se)
		{
			System.out.println("ERROR: unable to read input file.");
			System.exit(0);
		}

		return fis;
	}

	/**
	 * Closes the file to further I/O operations.
	 */
	void closeFile()
	{
		try
		{
			fis.close();
		}
		catch(IOException e){} // file input stream already closed
	}

	/**
	 * Returns the next byte in the file.
	 * @return   An integer representation of the next byte in the file. 
	 */
	int getNextByte()
	{
		try
		{
			return fis.read();
		}
		catch(IOException e)
		{
			System.out.println("ERROR: unexpected error encountered during file read.");
			return -1;
		}

	}

}
